﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.SharePoint;
using SPContrib.Artifacts.Definitions;

namespace SPContrib.Artifacts.Collections
{
    /// <summary>
    /// A collection of <see cref="FieldRefDefinition"/> elements
    /// </summary>
    public class FieldRefDefinitionCollection : IEnumerable<FieldRefDefinition>
    {
        private List<FieldRefDefinition> _fieldreferences;
        private ContentTypeDefinition _parent;

        /// <summary>
        /// TODO: document
        /// </summary>
        public IEnumerable<FieldRefDefinition> Additions
        {
            get
            {
                return from fieldref in _fieldreferences
                       where fieldref.Remove == false
                       select fieldref;
            }
        }

        /// <summary>
        /// TODO: document
        /// </summary>
        public IEnumerable<FieldRefDefinition> Removals
        {
            get
            {
                return from fieldref in _fieldreferences
                       where fieldref.Remove
                       select fieldref;
            }
        }

        internal FieldRefDefinitionCollection(ContentTypeDefinition parent, IEnumerable<XElement> fieldrefs)
        {
            this._parent = parent;
            this._fieldreferences = new List<FieldRefDefinition>
            (
                from fieldref in fieldrefs
                select new FieldRefDefinition(this, fieldref)
            );
        }


        public IEnumerator<FieldRefDefinition> GetEnumerator()
        {
            return _fieldreferences.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _fieldreferences.GetEnumerator();
        }
        
    }
}
